\documentclass[DaoFP]{subfiles}
\begin{document}
\setcounter{chapter}{20}

\chapter{Enrichment}

Lao Tzu says: "To know you have enough is to be rich." 

\section{Enriched Categories}

This might come as a surprise, but the Haskell definition of a \hask{Functor} cannot be fully explained without some background in enriched categories. In this chapter I'll try to show that, at least conceptually, enrichment is not a huge step from the ordinary category theory. 

Additional motivation for studying enriched categories comes from the fact that a lot of literature, notably the website nLab, contains descriptions of concepts in most general terms, which often means in terms of enriched categories. Most of the usual constructs can be translated just by changing the vocabulary, replacing hom-sets with hom-objects and $\Set$ with a monoidal category $\cat V$. 

Some enriched concepts, like weighted limits and colimits, turn out to be powerful on their own, to the extent that one might be tempted to replace Mac Lane's adage,``All concepts are Kan extensions'' with ``All concepts are weighted (co-)limits.''

\subsection{Set-theoretical foundations}

Category theory is very frugal at its foundations. But it (reluctantly) draws upon set theory. In particular the idea of the hom-set, defined as a set of arrows between two objects, drags in set theory as the prerequisite to category theory. Granted, arrows form a set only in a \emph{locally small} category, but that's a small consolation, considering that dealing with things that are too big to be sets requires even more theory. 

It would be nice if category theory were able to bootstrap itself, for instance by replacing hom-sets with more general objects. That's exactly the idea behind enriched categories. These hom-object, though, have to come from some other category that has hom-sets and, at some point we have to fall back on set-theoretical foundations. Nevertheless, having the option of replacing stuctureless hom-sets with something different expands our ability to model more complex systems.

The main property of sets is that, unlike objects, they are not atomic: they have \emph{elements}. In category theory we sometimes talk about \emph{generalized elements}, which are simply arrows pointing at an object; or \emph{global elements}, which are arrows from the terminal object (or, sometimes, from the monoidal unit $I$). But most importantly, sets define \emph{equality of elements}. 

Virtually all that we've learned about categories can be translated into the realm of enriched categories. However, a lot of categorical reasoning involves commuting diagrams, which express the equality of arrows. In the enriched setting we don't have arrows going between objects, so all these constructions will have to be modified.

\subsection{Hom-Objects}

At first sight, replacing hom-sets with objects might seem like a step backward. After all, sets have elements, while objects are formless blobs. However, the richness of hom-objects is encoded in the morphisms of the category they come from. Conceptually, the fact that sets are structure-less means that there are lots of morphisms (functions) between them. Having fewer morphisms often means having more structure. 

The guiding principle in defining enriched categories is that we should be able to recover ordinary category theory as a special case. After all hom-sets \emph{are} objects in the category $\Set$. In fact we've worked really hard to express properties of sets in terms of functions rather than elements. 

Having said that, the very definition of a category in terms of composition and identity involves morphisms that are \emph{elements} of hom-sets. So let's first re-formulate the primitives of a category without recourse to elements. 

Composition of arrows can be defined in bulk as a function between hom-sets:
\[ \circ \colon \mathcal C (b, c) \times \mathcal C (a, b) \to \mathcal C (a, c) \]

Instead of talking about the identity arrow, we can use a function from the singleton set:
\[ j_a \colon 1 \to \mathcal C (a, a) \]

This shows us that, if we want to replace hom-sets $\mathcal C (a, b)$ with objects from some category $\mathcal V$, we have to be able to \emph{multiply} these objects to define composition, and we need some kind of \emph{unit object} to define identity. We could ask for $\mathcal V$ to be cartesian but, in fact, a monoidal category works just fine. As we'll see, the unit and associativity laws of a monoidal category translate directly to identity and associativity laws for composition. 

\subsection{Enriched Categories}

Let $\mathcal V$ be a monoidal category with a tensor product $\otimes$, a unit object $I$, and the associator and two unitors (as well as their inverses):
\begin{align*}
\alpha &\colon (a \otimes b) \otimes c \to a \otimes (b \otimes c)
\\
 \lambda &\colon I \otimes a \to a
 \\
 \rho &\colon a \otimes I \to a
\end{align*}
A category $\mathcal C$ enriched over $\mathcal V$ has objects and, for any pair of objects $a$ and $b$, a hom-object $\mathcal C(a, b)$. This hom-object is an object in $\mathcal V$. Composition is defined using arrows in $\mathcal V$:
\[ \circ \colon \mathcal C (b, c) \otimes \mathcal C (a, b) \to \mathcal C (a, c) \]
Identity is defined by the arrow:
\[ j_a \colon I \to \mathcal C (a, a) \]
Associativity is expressed in terms of the associators in $\mathcal V$:
\[
 \begin{tikzcd}
 \big( \mathcal C (c, d) \otimes \mathcal C (b, c) \big) \otimes \mathcal C(a, b)
 \arrow[rr, "\alpha"]
 \arrow[d, "\circ \otimes id"]
 &&  \mathcal C (c, d) \otimes \big( \mathcal C (b, c) \otimes \mathcal C(a, b) \big) 
 \arrow[d, "id \otimes \circ"]
 \\
 \mathcal C(b, d) \otimes \mathcal C(a, b)
 \arrow[dr, "\circ"]
 && \mathcal C(c, d) \otimes \mathcal C(a, c)
 \arrow[dl, "\circ"']
 \\
 & \mathcal C(a, d)
 \end{tikzcd}
\]
Unit laws are expressed in terms of unitors in $\mathcal V$:

\[
 \begin{tikzcd}
 I \otimes \mathcal C(a, b)
 \arrow[r, "\lambda"]
 \arrow[d, "j_b \otimes id"']
 & \mathcal C(a, b)
 \\
 \mathcal C(b, b) \otimes \mathcal C(a, b)
 \arrow[ru, "\circ"]
 \end{tikzcd}
 \qquad
\begin{tikzcd}
\mathcal C(a, b) \otimes I
\arrow[r, "\rho"]
\arrow[d, "id \otimes j_a"']
& \mathcal C(a, b)
\\
\mathcal C(a, b) \otimes \mathcal C(a, a)
\arrow[ru, "\circ"]
 \end{tikzcd}
\]
Notice that these are all diagrams in $\mathcal V$, where we do have arrows forming hom-sets. We still fall back on set theory, but at a different level.

A category enriched over $\mathcal V$ is also called a $\mathcal V$-category. In what follows we'll assume that the enriching category is \emph{symmetric} monoidal, so we can form opposite and product $\cat V$-categories.


The category $\mathcal C^{op}$ opposite to a $\mathcal V$-category $\mathcal C$ is obtained by reversing hom-objects, that is:
\[ \mathcal C^{op}(a, b) = \mathcal C(b, a) \]
Composition in the opposite category involves reversing the order of hom-objects, so it only works if the tensor product is symmetric.

We can also define a tensor product of $\cat V$-categories; again, provided that $\mathcal V$ is symmetric. The product of two $\mathcal V$-categories $\mathcal C \otimes \mathcal D$ has, as objects, pairs of objects, one from each category. The hom-objects between such pairs are defined to be tensor products:
\[ \mathcal (C \otimes \mathcal D) (\langle c, d \rangle, \langle c', d' \rangle) = \mathcal C(c, c') \otimes \mathcal D (d, d') \]
We need symmetry of the tensor product in order to define composition. Indeed, we need to swap the two hom-objects in the middle, before we can apply the two available compositions:
\[ \circ \colon  \big(\mathcal C(c', c'',) \otimes \mathcal D (d', d'')\big) \otimes \big( \mathcal C(c, c') \otimes \mathcal D (d, d')\big) \to  \mathcal C(c, c'') \otimes \mathcal D (d, d'') \]
The identity arrow is the tensor product of two identities:
\[ I_{\mathcal C} \otimes I_{\mathcal D} \xrightarrow{j_c \otimes j_d} \mathcal C(c, c) \otimes \mathcal D (d, d) \]


\begin{exercise}
Define composition and unit in the $\mathcal V$-category $\mathcal C^{op}$.
\end{exercise}

\begin{exercise}
Show that every $\mathcal V$-category $\mathcal C$ has an underlying ordinary category $\mathcal C_0$ whose objects are the same, but whose hom-sets are given by (monoidal global) elements of the hom-objects, that is elements of  $\mathcal V(I, \mathcal C(a, b))$.
\end{exercise}

\subsection{Examples}

Seen from this new perspective, the ordinary categories we've studied so far were trivially enriched over the monoidal category $(\Set, \times, 1)$, with the cartesian product as the tensor product, and the singleton set as the unit. 

Interestingly, a 2-category can be seen as enriched over $\mathbf{Cat}$. Indeed, 1-cells in a 2-category are themselves objects in another category. The 2-cells are just arrows in that category. In particular the 2-category $\mathbf{Cat}$ of small categories is enriched in itself. Its hom-objects are functor categories, which are objects in $\mathbf{Cat}$.

\subsection{Preorders}

Enrichment doesn't always mean adding more stuff. Sometimes it looks more like impoverishment, as is the case of enriching over a walking arrow category. 

This category has just two objects which, for the purpose of this construction, we'll call $\text{False}$ and $\text{True}$. There is a single arrow from $\text{False}$ to $\text{True}$ (not counting identity arrows), which makes $\text{False}$ the initial object and $\text{True}$ the terminal one. 
\[
 \begin{tikzcd}
 \text{False}
 \arrow[r, "!"]
 \arrow[loop, "id_{\text{False}}"']
 & \text{True}
 \arrow[loop, "id_{\text{True}}"']
 \end{tikzcd}
\]
To make this into a monoidal category, we define the tensor product, such that:
\[ \text{True} \otimes \text{True} = \text{True} \]
and all other combinations produce \text{False}.
$\text{True}$ is the monoidal unit, since:
\[ \text{True} \otimes x = x \]

A category enriched over the monoidal walking arrow is called a \emph{preorder}. A hom-object $\mathcal C (a, b)$ between any two objects can be either $\text{False}$ or $\text{True}$. We interpret $\text{True}$ to mean that $a$ precedes $b$ in the preorder, which we write as $a \le b$. $\text{False}$ means that the two objects are unrelated. 

The important property of composition, as defined by:
\[ \mathcal C (b, c) \otimes \mathcal C (a, b) \to \mathcal C (a, c) \]
is that, if both hom-objects on the left are $\text{True}$, then the right hand side must also be $\text{True}$. (It can't be $\text{False}$, because there is no arrow going from $\text{True}$ to $\text{False}$.) In the preorder interpretation, it means that $\le$ is transitive:
\[ b \le c \land a \le b \implies a \le c \]

By the same reasoning, the existence of the identity arrow:
\[ j_a \colon \text{True} \to \mathcal C(a, a) \]
means that $\mathcal C(a, a)$ is always $\text{True}$. In the preorder interpretation, this means that $\le$ is reflexive, $a \le a$.

Notice that a preorder doesn't preclude cycles and, in particular, it's possible to have $a \le b$ and $b \le a$ without $a$ being equal to $b$. 

A preorder may also be defined without resorting to enrichment as a \index{thin category}\emph{thin category}---a category in which there is at most one arrow between any two objects.

\subsection{Self-enrichment}

Any cartesian closed category $\mathcal V$ can be viewed as self-enriched. This is because every external hom-set $\mathcal C(a, b)$ can be replaced by the internal hom $b^a$ (the object of arrows). 

In fact every \emph{monoidal closed} category $\mathcal V$ is self-enriched. Recall that, in a monoidal closed category we have the hom-functor adjunction:
\[ \mathcal V (a \otimes b, c) \cong \mathcal V (a, [b, c]) \]
The counit of this adjunction works as the evaluation morphism:
\[ \varepsilon_{b c} \colon [b, c] \otimes b \to c \]

To define composition in this self-enriched category, we need an arrow:
\[ \circ \colon [b, c] \otimes [a, b] \to [a, c] \]
The trick is to consider the whole hom-set at once and show that we can always pick a canonical element in it. We start with the set:
\[ \mathcal V([b, c] \otimes [a, b], [a, c]) \]
We can use the adjunction to rewrite it as:
\[  \mathcal V( ([b, c] \otimes [a, b]) \otimes a, c) \]
All we have to do now is to pick an element of this hom-set. We do it by constructing the following composite:
\[ ([b, c] \otimes [a, b]) \otimes a \xrightarrow{\alpha}  
    [b, c] \otimes ([a, b] \otimes a) \xrightarrow{id \otimes \varepsilon_{a b} }
    [b, c] \otimes b \xrightarrow{\varepsilon_{b c}} c \]
We used the associator and the counit of the adjunction.

We also need an arrow that defines the identity:
\[ j_a \colon I \to [a, a] \]
Again, we can pick it as a member of the hom-set $\mathcal V(I, [a, a])$. We use the adjunction:
\[ \mathcal V(I, [a, a]) \cong \mathcal V (I \otimes a, a) \]
We know that this hom-set contains the left unitor $\lambda$, so we can use it to define $j_a$.

\section{$\mathcal V$-Functors} 

An ordinary functor maps objects to objects and arrows to arrows. Similarly, an enriched functor $F$ maps object to objects, but instead of acting on individual arrows, it must map hom-objects to hom-objects. This is only possible if the hom-objects in the source category $\mathcal C$ belong to the same category as the hom-objects in the target category $\mathcal D$. In other words, both categories must be enriched over the same $\mathcal V$. The action of $F$ on hom-objects is then defined using arrows in $\mathcal V$:
\[ F_{a b} \colon \mathcal C (a, b) \to \cat D (F a, F b) \]
For clarity we specify the pair of objects in the subscript of $F$.

A functor must preserve composition and identity. These can be expressed as commuting diagrams in $\mathcal V$:

\[
 \begin{tikzcd}
 \mathcal C(b, c) \otimes \mathcal C(a, b) 
 \arrow[r, "\circ"]
 \arrow[d, "F_{b c} \otimes F_{a b}"]
 & \mathcal C(a, c)
 \arrow[d, "F_{a c}"]
 \\
 \mathcal D(F b, F c) \otimes \mathcal D (F a, F b)
 \arrow[r, "\circ"]
 & \mathcal D(F a, F b)
 \end{tikzcd}
 \qquad
 \begin{tikzcd}
 & I
 \arrow[dl, "j_a"']
 \arrow[dr, "j_{F a}"]
 \\
 \mathcal C(a, a)
 \arrow[rr, "F_{a a}"]
 && \mathcal D( F a, F a)
  \end{tikzcd}
\]
Notice that I used the same symbol $\circ$ for two different compositions and the same $j$ for two different identity mappings. Their meaning can be derived from the context.

As before, all diagrams are in the category $\mathcal V$.

\subsection{The \text{Hom}-functor}

The hom-functor in a category that is enriched over a monoidal \emph{closed} category $\mathcal V$ is an enriched functor:
\[ \text{Hom}_{\cat C} \colon \mathcal C^{op} \otimes \mathcal C \to \mathcal V \]
Here, in order to define an enriched functor, we have to treat $\mathcal V$ as self-enriched. 

It's clear how this functor works on (pairs of) objects:
\[ \text{Hom}_{\cat C} \langle a, b \rangle = \mathcal C (a, b) \]


To define an enriched functor, we have to define the action of $\text{Hom}$ on hom-objects. Here, the source category is $\mathcal C^{op} \otimes \mathcal C$ and the target category is $\cat V$, both enriched over $\cat V$. Let's consider a hom-object from $\langle a, a' \rangle$ to $\langle b, b' \rangle$. The action of the hom-functor on this hom-object is an arrow in $\cat V$:
\[ \text{Hom}_{\langle a, a' \rangle \langle b, b' \rangle} \colon (C^{op} \otimes \mathcal C)(\langle a, a' \rangle, \langle b, b' \rangle) \to \mathcal V (\text{Hom}\langle a, a' \rangle, \text{Hom}\langle b, b' \rangle)\]
By definition of the product category, the source is a tensor product of two hom-objects. The target is the internal hom in $\mathcal V$. We are thus looking for an arrow:
\[ \mathcal C(b, a) \otimes \mathcal C(a', b') \to [\mathcal C(a, a'), \mathcal C(b, b')] \]
We can use the currying hom-functor adjunction to unpack the internal hom:
\[ \Big( \mathcal C(b, a) \otimes \mathcal C(a', b') \Big) \otimes \mathcal C(a, a') \to \mathcal C(b, b') \]
We can construct this arrow by rearranging the product and applying the composition twice.

In the enriched setting, the closest we can get to defining an individual morphism from $a$ to $b$ is to use an arrow from the unit object. We define a (monoidal-global) element of a hom-object as a morphism in $\cat V$:
\[ f \colon I \to \mathcal C(a, b) \]
We can define what it means to lift such an arrow using the hom-functor. For instance, keeping the first argument constant, we'd define:
\[ \mathcal C(c, f) \colon \mathcal C(c, a) \to C(c, b) \] 
as the composite:
\[ \mathcal C(c, a) \xrightarrow{\lambda^{-1}} I \otimes \mathcal C(c, a) \xrightarrow{f \otimes id} \mathcal C(a, b) \otimes \mathcal C(c, a) \xrightarrow{\circ} \mathcal C(c, b) \]
Similarly, the contravariant lifting of $f$:
\[ \mathcal C(f, c) \colon \mathcal C(b, c) \to \mathcal C(a, c) \]
can be defined as:
\[ \mathcal C(b, c) \xrightarrow{\rho^{-1}} \mathcal C(b, c) \otimes I \xrightarrow{id \otimes f} \mathcal C (b, c) \otimes \mathcal C(a, b) \xrightarrow{\circ} \mathcal C(a, c) \]

A lot of the familiar constructions we've studied in ordinary category theory have their enriched counterparts, with products replaced by tensor products and $\Set$ replaced by $\mathcal V$.

\begin{exercise}
What is a functor between two preorders?
\end{exercise}

\subsection{Enriched co-presheaves}
Co-presheaves, that is $\Set$-valued functors, play an important role in category theory, so it's natural to ask what their counterparts are in the enriched setting. The generalization of a co-presheaf is a $\cat V$-functor $\cat C \to \cat V$. This is only possible if $\cat V$ can be made into a $\cat V$-category, that is when it's monoidal-closed. 

An enriched co-presheaf maps object of $\cat C$ to objects of $\cat V$ and it maps hom-objects of $\cat C$ to internal homs of $\cat V$:
\[ F_{a b} \colon \mathcal C (a, b) \to \cat [F a, F b] \]

In particular, the $\text{Hom}$-functor is an example of a $\cat V$-valued $\cat V$-functor:
\[ \text{Hom} \colon \cat C^{op} \otimes \cat C \to \cat V \]

The hom-functor is a special case of an enriched profunctor, which is defined as:
\[ \mathcal C^{op} \otimes \mathcal D \to \mathcal V \]

\begin{exercise}
The tensor product is a functor in $\cat V$:
\[ \otimes \colon \cat V \times \cat V \to \cat V \]
Show that if $\cat V$ is monoidal closed, the tensor product defines a $\cat V$-functor. Hint: Define its action on internal homs.
\end{exercise}

\subsection{Functorial strength and enrichment}

When we were discussing monads, I mentioned an important property that made them work in programming. The endofunctors that define monads must be strong, so that we can access external contexts inside monadic code. 

It turns out that the way we have defined endofunctors in Haskell makes them automatically strong. The reason is that strength is related to enrichment and, as we've seen, a cartesian closed category is self-enriched. Let's start with some definitions.

Functorial strength for an endofunctor $F$ in a monoidal category is defined as a natural transformation with components:
\[ \sigma_{a b} \colon a \otimes F(b) \to F (a \otimes b) \]
There are some pretty obvious coherence conditions that make strength respect the properties of the tensor product. This is the associativity condition:
\[
 \begin{tikzcd}
 (a \otimes b) \otimes F (c) 
 \arrow[rr, "\sigma_{(a \otimes b) c}"]
 \arrow[d, "\alpha"]
 && F((a \otimes b) \otimes c)
 \arrow[d, "F(\alpha)"]
 \\
 a \otimes (b \otimes F(c))
 \arrow[r, "a \otimes \sigma_{b c}"]
 & a \otimes F(b \otimes c)
 \arrow[r, "\sigma_{a (b \otimes c)}"]
 & F(a \otimes (b \otimes c))
 \\
 \end{tikzcd}
\]
and this is the unit condition:
\[
 \begin{tikzcd}
 I \otimes F(a)
 \arrow[r, "\sigma_{I a}"]
 \arrow[rd, "\lambda"]
 & F(I \otimes a)
 \arrow[d, "F (\lambda)"]
 \\
 & F(a)
 \end{tikzcd}
\]

In a general monoidal category this is called the \emph{left strength}, and there is a corresponding definition of the right strength. In a symmetric monoidal category, the two are equivalent.

An enriched endofunctor maps hom-objects to hom-objects:
\[ F_{a b} \colon \cat C (a, b) \to \cat C (F a, F b) \]
If we treat a monoidal closed category $\cat V$ as self-enriched, the hom-objects are internal homs, so an enriched endofunctor is equipped with the mapping:
\[ F_{a b} \colon [a, b] \to [F a, F b] \]
Compare this with our definition of a Haskell \hask{Functor}:
\begin{haskell}
class Functor f where
  fmap :: (a -> b) -> (f a -> f b)
\end{haskell}
The function types involved in this definition, \hask{(a -> b)} and \hask{(f a -> f b)}, are the \emph{internal} homs. So a Haskell \hask{Functor} is indeed an enriched functor. 

We don't normally distinguish between external and internal homs in Haskell, since their sets of elements are isomorphic. It's a simple consequence of the currying adjunction:
\[ \cat C(1 \times b, c) \cong \cat C(1, [b, c]) \]
and the fact that the terminal object is the unit of the cartesian product.

It turns out that in a self-enriched category $\cat V$ every strong endofunctor is automatically enriched. Indeed, to show that a functor $F$ is enriched we need to define the mapping between internal homs, that is an element of the hom-set:
\[ F_{a b} \in \cat V([a, b], [F a, F b]) \]
Using the hom adjunction, this is isomorphic to:
\[ \cat V([a, b] \otimes F a, F b) \]
We can construct this mapping by composing the strength and the counit of the adjunction (the evaluation morphism):
\[ [a, b] \otimes F a \xrightarrow{\sigma_{[a, b] a}} F ([a, b] \otimes a) \xrightarrow{F \epsilon_{a b}} F b \]

Conversely, every enriched endofunctor in $\cat V$ is strong. To show strength, we need to define the mapping $\sigma_{a b}$, or equivalently (by hom-adjunction):
\[ a \to [F b, F (a \otimes b)] \]
Recall the definition of the unit of the hom adjunction, the coevaluation morphism:
\[ \eta_{a b} \colon a \to [b, a \otimes b] \]
We construct the following composite:
\[ a \xrightarrow{\eta_{a b}} [b, a \otimes b] \xrightarrow{F_{b, a \otimes b}} [F b, F (a \otimes b)] \]

This can be translated directly to Haskell:
\begin{haskell}
strength :: Functor f => (a, f b) -> f (a, b)
strength = uncurry (\a -> fmap (coeval a))
\end{haskell}
with the following definition of \hask{coeval}:
\begin{haskell}
coeval :: a -> (b -> (a, b))
coeval a = \b -> (a, b)
\end{haskell}
Since currying and evaluation are built into Haskell, we can further simplify this formula:
\begin{haskell}
strength :: Functor f => (a, f b) -> f (a, b)
strength (a, bs) = fmap (a, ) bs
\end{haskell}

\section{$\mathcal V$-Natural Transformations}

An ordinary natural transformation between two functors $F$ and $G$ from $\mathcal C$ to $\mathcal D$ is a selection of arrows from the hom-sets $\mathcal D(F a, G a)$. In the enriched setting, we don't have arrows, so the next best thing we can do is to use the unit object $I$ to do the selection. We define a component of a $\cat V$-natural transformation at $a$ as an arrow:
\[ \nu_a \colon I \to \mathcal D(F a, G a) \]

Naturality condition is a little tricky. The standard naturality square involves the lifting of an arbitrary arrow $f \colon a \to b$ and the equality of the following compositions:
\[ \nu_b \circ F f = G f \circ \nu_a \]

Let's consider the hom-sets that are involved in this equation. We are lifting a morphism $f \in \mathcal C(a, b)$. The composites on both sides of the equation are the elements of $\mathcal D(F a, G b)$. 

On the left, we have the arrow $ \nu_b \circ F f$. The composition itself is a mapping from the product of two hom-sets:
\[  \mathcal D(F b, G b) \times \mathcal D(F a, F b) \to \mathcal D(F a, G b) \]
Similarly, on the right we have $G f \circ \nu_a$, which a composition:
\[ \mathcal D(G a, G b) \times \mathcal D(F a, G a) \to  \mathcal D(F a, G b) \]

In the enriched setting we have to work with hom-objects rather than hom-sets, and the selection of the components of the natural transformation is done using the unit $I$. We can always produce the unit out of thin air using the inverse of the left or the right unitor. 

Altogether, the naturality condition is expressed as the following commuting diagram:
\[
 \begin{tikzcd}
 & I \otimes \mathcal C(a, b)
 \arrow[r, "\nu_b \otimes F_{a b}"]
 & \mathcal D(F b, G b) \otimes \mathcal D(F a, F b)
 \arrow[dr, "\circ"]
 \\
 \mathcal C(a, b)
 \arrow[ur, "\lambda^{-1}"]
\arrow[dr, "\rho^{-1}"']
 &&& \mathcal D(F a, G b)
 \\
 & \mathcal C(a, b) \otimes I 
 \arrow[r, "G_{a b} \otimes \nu_a"]
 & \mathcal D(G a, G b) \otimes \mathcal D(F a, G a)
 \arrow[ur, "\circ"']
  \end{tikzcd}
\]
This also works for an ordinary category, where we can trace two paths through this diagram by first picking an $f$ from $\cat C(a, b)$. We can then use $\nu_b$ and $\nu_a$ to pick components of the natural transformation. We also lift $f$ using either $F$ or $G$. Finally, we use composition to reproduce the naturality equation.

This diagram can be further simplified if we use our earlier definition of  the hom-functor's action on global elements of hom-objects. The components of a natural transformation are defined as such global elements:
\[ \nu_a \colon I \to \mathcal D(F a, G a) \]
There are two such liftings at our disposal:
\[ \mathcal D(d, \nu_b) \colon \mathcal D(d, F b) \to \mathcal D(d, G b) \]
and:
\[ \mathcal D (\nu_a, d) \colon \mathcal D(G a, d) \to \mathcal D(F a, d) \]
We get something that looks more like the familiar naturality square:
\[
 \begin{tikzcd}
 & \mathcal D(F a, F b)
  \arrow[dr, "{\mathcal D (F a, \nu_b)}"]
 \\
 \mathcal C(a, b) 
 \arrow[ur, "F_{a b}"]
 \arrow[dr, "G_{a b}"']
 && \mathcal D(F a, G b)
 \\
& \mathcal D (G a, G b)
\arrow[ur, "{\mathcal D (\nu_a, G b)}"']
 \end{tikzcd}
\]


$\mathcal V$-natural transformations between two $\mathcal V$-functors $F$ and $G$ form a set we call $\mathcal V\text{-nat} (F, G)$. 

Earlier we have seen that, in ordinary categories, the set of natural transformations can be written as an end:
\[ [\mathcal C, \mathcal D](F, G) \cong \int_a \mathcal D(F a, G a) \]
It turns out that ends and coends can be defined for enriched profunctors, so this formula works for enriched natural transformations as well. The difference is that, instead of a \emph{set} of natural transformations $\mathcal V\text{-nat} (F, G)$, it defines the \emph{object} of natural transformations $[\mathcal C, \mathcal D](F, G)$ in $\mathcal V$. 

The definition of the (co-)end of a $\cat V$-profunctor $P \colon \cat C \otimes \cat C^{op} \to \cat V$ is analogous to the definition we've seen for ordinary profunctors. For instance, the end is an object $e$ in $\cat V$ equipped with an extranatural transformation $\pi \colon e \to P$ that is universal among such objects. 

\section{Yoneda Lemma}

The ordinary Yoneda lemma involves a $\Set$-valued functor $F$ and a set of natural transformations:
\[ [\mathcal C, \Set]( \mathcal C(c, -), F) \cong F c \]
To generalize it to the enriched setting, we'll consider a $\mathcal V$-valued functor $F$. As before, we'll use the fact that we can treat $\mathcal V$ as self-enriched, as long as it's closed, so we can talk about $\mathcal V$-valued $\mathcal V$-functors. 

The weak version of the Yoneda lemma deals with a \emph{set} of $\mathcal V$-natural transformations. Therefore, we have to turn the right hand side into a set as well. This is done by taking the (monoidal-global) elements of $F c$. We get:
\[ \mathcal V\text{-nat} ( \mathcal C(c, -), F) \cong \mathcal V(I, F c) \]

The strong version of the Yoneda lemma works with objects of $\mathcal V$ and uses the end over the internal hom in $\mathcal V$ to represent the object of natural transformations:
\[ \int_x [\mathcal C( c, x), F x] \cong F c \]

\section{Weighted Limits}

Limits (and colimits) are built around commuting triangles, so they are not immediately translatable to the enriched setting. The problem is that cones are constructed from ``wires,'' that is individual morphisms. You may think of hom-sets as a thick bundle of wires, each wire having zero thickness. When constructing a cone, you are selecting a single wire from a given hom-set. We have to replace wires with something thicker.

Consider a diagram, that is a functor $D$ from the indexing category $\mathcal J$ to the target category $\mathcal C$. The wires for the cone with the apex $x$ are selected from hom-sets $\mathcal C(x, D j)$, where $j$ is an object of $\mathcal J$. 

\[
 \begin{tikzcd}
 \\
 \\
j
\arrow[rr, red]
\arrow[rd, red]
&& k
\arrow[dl, red]
\\
& l
 \end{tikzcd}
 \qquad
 \begin{tikzcd}
  & x
 \arrow[ddl, ""']
 \arrow[ddl, bend right, "{\mathcal C(x, D j)}"']
 \arrow[ddl, bend left, ""]
 \\
\\
D j
\arrow[rr, red]
\arrow[rd, red]
&& D k
\arrow[dl, red]
\\
& D l
 \end{tikzcd}
 \]
This selection of a $j$'th wire can be described as a function from the singleton set $1$:
\[ \gamma_j \colon 1 \to \mathcal C(x, D j) \]
We can try to gather these functions into a natural transformation:
\[ \gamma \colon \Delta_1 \to \mathcal C(x, D -) \]
where $\Delta_1$ is a constant functor mapping all objects of $\mathcal J$ to the singleton set. Naturality conditions ensure that the triangles forming the sides of the cone commute. 

The set of all cones  with the apex $x$ is then given by the set of natural transformations:
\[ [\mathcal J, \Set] (\Delta_1, \mathcal C(x, D-)) \]

This reformulation gets us closer to the enriched setting, since it rephrases the problem in terms of hom-sets rather than individual morphisms. We could start by considering both $\mathcal J$ and $\mathcal C$ to be enriched over $\mathcal V$, in which case $D$ would be a $\mathcal V$-functor. 

There is just one problem: how do we define a constant $\mathcal V$-functor $\Delta_x \colon \cat C \to \cat D$? Its action on objects is obvious:  it maps all objects in $\cat C$ to one object $x$ in $\cat D$. But what should it do to hom-objects? 

An ordinary constant functor $\Delta_x$ maps all morphisms in $\cat C(a, b)$ to the identity in $id_x \in \cat D(x, x)$. In the enriched setting, though, $\cat D(x, x)$ is an object with no internal structure. Even if it happened to be the unit $I$, there's no guarantee that for every hom-object $\mathcal C(a, b)$ we could find an arrow to $I$; and even if there was one, it might not be unique. In other words, there is no reason to believe that $I$ is the terminal object.

The solution is to ``smear the singularity'': instead of using the constant functor to select a single wire, we should use some other ``weighting'' functor $W \colon \mathcal J\to \Set$ to select a thicker ``cylinder'' inside a hom-set. Such a weighted cone with the apex $x$ is an element of the set of natural transformations:
\[ [\mathcal J, \Set] \left(W, \mathcal C(x, D-)\right) \]

A \emph{weighted limit}, also known as an \index{indexed limit}\emph{indexed limit}, $\text{lim}^W D$, is then defined as the universal weighted cone. It means that for any weighted cone with the apex $x$ there is a unique morphism from $x$ to $\text{lim}^W D$ that factorizes it. The factorization is guaranteed by the naturality of the isomorphism that defines the weighted limit:
\[  \mathcal C(x, \text{lim}^W D) \cong [\mathcal J, \Set] (W, \mathcal C(x, D-)) \]

The regular, non-weighted limit is often called a \index{limit, conical}\emph{conical} limit, and it corresponds to using the constant functor as the weight. 

This definition can be translated almost verbatim to the enriched setting by replacing $\Set$ with $\mathcal V$:
\[  \mathcal C(x, \text{lim}^W D) \cong [\mathcal J, \mathcal V] (W, \mathcal C(x, D-)) \]
Of course, the meaning of the symbols in this formula is changed. Both sides are now objects in $\mathcal V$. The left-hand side is the hom-object in $\mathcal C$, and the right-hand side is the object of natural transformations between two $\mathcal V$-functors.

Dually, a weighted colimit is defined by the natural isomorphism:
\[  \mathcal C(\text{colim}^W D, x) \cong [\mathcal J^{op}, \mathcal V] (W, \mathcal C(D-, x)) \]
Here, the colimit is weighed by a functor $W \colon \mathcal J^{op} \to \mathcal V$ from the opposite category.

Weighted (co-)limits, both in ordinary and in enriched categories, play a fundamental role: they can be used to re-formulate a lot of familiar constructions, like (co-)ends, Kan extensions, etc. 

\section{Ends as Weighted Limits}

An end has a lot in common with a product or, more generally, with a limit. If you squint hard enough, the projections $\pi_x \colon e \to P \langle a, a \rangle$ form the sides of a cone; except that instead of commuting triangles we have wedges. It turns out that we can express ends as weighted limits. The advantage of this formulation is that it also works in the enriched setting. 

We've seen that the end of a $\cat V$-valued $\cat V$-profunctor can be defined using the more fundamental notion of an extranatural transformation. This in turn allowed us to define the \emph{object} of natural transformations, which enabled us to define weighted limits. We can now go ahead and extend the definition of the end to work with a more general $\cat V$-functor of mixed variance with values in a $\cat V$-category $\cat D$:
\[ P \colon \mathcal C^{op} \otimes \mathcal C \to \mathcal D \]
We'll use this functor as a diagram in $\cat D$. 

At this point mathematicians start worrying about size issues. After all we are embedding a whole category---squared---as a single diagram in $\cat D$. To avoid the size problems, we'll just assume that $\cat C$ is small; that is, its objects form a set. 

We want to take a weighted limit of the diagram defined by $P$. The weight must be a $\mathcal V$-functor $\mathcal C^{op} \otimes \mathcal C \to \cat V$. There is one such functor that's always at our disposal, the hom-functor $\text{Hom}_{\cat C}$. We will use it to define the end as a weighted limit:
\[  \int_c P\langle c, c\rangle = \text{lim}^{\text{Hom}} P\]

First, let's convince ourselves that this formula works in an ordinary ($\Set$-enriched) category. Since ends are defined by their mapping-in property, let's consider a mapping from an arbitrary object $d$ to the weighted limit and use the standard Yoneda trick to show the isomorphism. By definition, we have:
\[ \cat D(d, \text{lim}^{\text{Hom}} P) \cong [\cat C^{op} \times \cat C, \Set](\cat C(-, =), \cat D (d, P(-, =))\]
We can rewrite the set of natural transformations as an end over pairs of objects $\langle c, c' \rangle$:
\[ \int_{\langle c, c' \rangle} \Set(\cat C(c, c'), \cat D (d, P\langle c, c' \rangle)) \]
Using the Fubini theorem, this is equivalent to the iterated end:
\[\int_c \int_{c'} \Set(\cat C(c, c'), \cat D(d, P\langle c, c' \rangle))\]
We can now apply the ninja-Yoneda lemma to perform the integration over $c'$. The result is:
\[ \int_c \cat D(d, P\langle c, c \rangle) \cong \cat D(d, \int_c P \langle c, c \rangle) \]
where we used continuity to push the end under the hom-functor. Since $d$ was arbitrary, we conclude that, for ordinary categories:
\[ \text{lim}^{\text{Hom}} P \cong  \int_c P\langle c, c\rangle \]
This justifies our use of the weighed limit to define the end in the enriched case.

An analogous formula works for the coend, except that we use the colimit with the hom-functor in the opposite category $\text{Hom}_{\cat C^{op}}$ as the weight:
\[  \int^c P\langle c, c\rangle = \text{colim}^{\text{Hom}_{\cat C^{op}}} P\]

\begin{exercise}
Show that for ordinary $\Set$-enriched categories the weighted colimit definition of a coend reproduces the earlier definition. Hint: use the mapping out property of the coend.
\end{exercise}

\begin{exercise}
Show that, as long as both sides exist, the following identities hold in ordinary ($\Set$-enriched) categories (they can be generalized to the enriched setting):
\[ \text{lim}^W D \cong \int_{j \colon \cat J} W j \pitchfork D j \]
\[ \text{colim}^W D \cong \int^{j \colon \cat J} W j \cdot D j \]
Hint: Use the mapping in/out with the Yoneda trick and the definition of power and copower.
\end{exercise}

\section{Kan Extensions}

We've seen how to express limits and colimits as Kan extensions using a functor from the singular category $1$. Weighted limits let us dispose of the singularity, and a judicious choice of the weight lets us express Kan extensions in terms of weighted limits. 

First, let's work out the formula for ordinary, $\Set$-enriched categories. The right Kan extension is defined as:
 \[ (\text{Ran}_P F) e \cong \int_c \cat B (e, P c) \pitchfork F c \]
 We'll consider the mapping into it from an arbitrary object $d$. The derivation follows a number of simple steps, mostly by expanding the definitions. 
 
 We start with:
\[ \cat D\big(d, (\text{Ran}_P F) e\big) \]
 and substitute the definition of the Kan extension:
 \[ \cat D\big(d, \int_c \cat B (e, P c) \pitchfork F c\big) \]
Using the continuity of the hom-functor, we can pull out the end:
 \[ \int_c \cat D\big(d,  \cat B (e, P c) \pitchfork F c\big) \]
We then use the definition of the pitchfork:
\[ \cat D (d, A \pitchfork d') \cong \Set  \big(A, \cat D(d, d')\big) \]
to get:
 \[ \int_c \cat D\big(d,  \cat B (e, P c) \pitchfork F c\big) \cong \int_c \Set\big( \cat B(e, P c), \cat D(d, F c)\big)\]
This can be written as a set of natural transformation:
\[ [\cat C, \Set] \big(\cat B(e, P-), \cat D(d, F-)\big) \]
The weighted limit is also defined through the set of natural transformations:
\[  \mathcal D(d, \text{lim}^W F) \cong [\mathcal C, \Set] \big(W, \mathcal D(d, F-)\big) \]
leading us to the final result:
\[ \cat D(d, \text{lim}^{\cat B (e, P-)} F) \]
Since $d$ was arbitrary, we can use the Yoneda trick to conclude that:
\[ (\text{Ran}_P F) e = \text{lim}^{\cat B(e, P-)} F  \]
This formula becomes the definition of the right Kan extension in the enriched setting.

Similarly, the left Kan extension can be defined as a weighted colimit:
\[ (\text{Lan}_P F) e = \text{colim}^{\cat B(P-, e)} F \]

\begin{exercise}
Derive the formula for the left Kan extension for ordinary categories.
\end{exercise}

\section{Useful Formulas}
\begin{itemize}
\item Yoneda lemma:
\[ \int_x [\mathcal C( c, x), F x] \cong F c \]
\item Weighted limit:
\[  \mathcal C(x, \text{lim}^W D) \cong [\mathcal J, \mathcal V] \big(W, \mathcal C(x, D-)\big) \]
\item Weighted colimit:
\[  \mathcal C(\text{colim}^W D, x) \cong [\mathcal J^{op}, \mathcal V] \big(W, \mathcal C(D-, x)\big) \]
\item Right Kan extension:
\[ (\text{Ran}_P F) e = \text{lim}^{\cat B(e, P-)} F  \]
\item Left Kan extension:
\[ (\text{Lan}_P F) e = \text{colim}^{\cat B(P-, e)} F \]
\end{itemize}

\end{document}